//
//  UIImage+Crop.m
//  IPDFCameraViewControllerDemo
//
//  Created by CuiPanJun on 15/8/27.
//  Copyright (c) 2015年 Maximilian Mackh. All rights reserved.
//

#import "UIImage+Crop.h"

@implementation UIImage (Crop)

- (UIImage *)croppedImageWithTransformedFrame:(CGRect)frame cropRect:(CGRect)cropRect{
    
    CGSize imageSize = self.size;
    if (imageSize.width == 0.0f || imageSize.height == 0.0f) {
        return self;
    }
    
    CGFloat scale = CGRectGetWidth(frame)/imageSize.width;
    CGFloat scaleY = CGRectGetHeight(frame) / imageSize.height;
    
    CGFloat x = (CGRectGetMinX(cropRect) - CGRectGetMinX(frame))/scale;
    CGFloat y = (CGRectGetMinY(cropRect) - CGRectGetMinY(frame))/scaleY;
    CGFloat w = CGRectGetWidth(cropRect)/scale;
    CGFloat h = CGRectGetHeight(cropRect)/scaleY;
    
    CGRect convertedRect = CGRectMake(x, y, w, h);
    CGRect imageBounds = CGRectMake(0, 0, imageSize.width, imageSize.height);
    
    CGRect resultImageRect = CGRectZero;
    
    if(!CGRectIntersectsRect(imageBounds, convertedRect)){
        return nil;
    }else{
        //resultImageRect = CGRectIntersection(imageBounds, convertedRect);
        
        CGFloat maxX = x + w;
        CGFloat maxY = y + h;
        
        if (x < 0) {
            x = 0;
        }
        
        if (y < 0) {
            y = 0;
        }
        
        if (maxX > imageSize.width) {
            maxX = imageSize.width;
        }
        
        if (maxY > imageSize.height) {
            maxY = imageSize.height;
        }
        
        resultImageRect = CGRectMake(x, y, maxX - x, maxY - y);
        
        CGImageRef imageRef = self.CGImage;
        CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, resultImageRect);
        CGSize size;
        size.width = resultImageRect.size.width;
        size.height = resultImageRect.size.height;
        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
        CGContextDrawImage(context, resultImageRect, subImageRef);
        UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
        CGImageRelease(subImageRef);
        UIGraphicsEndImageContext();
        return smallImage;
    }
}


+ (UIImage *)fixOrientation:(UIImage *)srcImg {
    if (srcImg.imageOrientation == UIImageOrientationUp) return srcImg;
    CGAffineTransform transform = CGAffineTransformIdentity;
    switch (srcImg.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, srcImg.size.width, srcImg.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, srcImg.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, srcImg.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
            break;
    }
    
    switch (srcImg.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, srcImg.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, srcImg.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationLeft:
        case UIImageOrientationRight:
            break;
    }
    
    CGContextRef ctx = CGBitmapContextCreate(NULL, srcImg.size.width, srcImg.size.height,
                                             CGImageGetBitsPerComponent(srcImg.CGImage), 0,
                                             CGImageGetColorSpace(srcImg.CGImage),
                                             CGImageGetBitmapInfo(srcImg.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (srcImg.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            CGContextDrawImage(ctx, CGRectMake(0,0,srcImg.size.height,srcImg.size.width), srcImg.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,srcImg.size.width,srcImg.size.height), srcImg.CGImage);
            break;
    }
    
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}


@end
